//
//  WaterFlowLayout.m
//  UILessonWaterFlow
//
//  Created by 赵永泉 on 15/9/17.
//  Copyright (c) 2015年 蓝欧科技. All rights reserved.
//

#import "WaterFlowLayout.h"

@interface WaterFlowLayout ()

/**
 *  瀑布流的列数
 */
@property (nonatomic, assign) NSUInteger columns;
@property (nonatomic, retain) NSMutableArray *frameArray;// 存放每一列的frame
@property (nonatomic, retain) NSMutableArray *attributesArray;
@end

@implementation WaterFlowLayout

- (NSMutableArray *)attributesArray
{
    if (_attributesArray == nil) {
        self.attributesArray = [NSMutableArray array];
    }
    return _attributesArray;
}

- (NSMutableArray *)frameArray
{
    if (_frameArray == nil) {
        self.frameArray = [NSMutableArray array];
    }
    return _frameArray;
}

- (instancetype)initWithNumbersOfColumns:(NSUInteger)columns
{
    if (self = [super init]) {
        self.columns = columns;
    }
    return self;
}

// 返回当前高度最小的列的frame
- (CGRect)minimumHeightOfIndex:(NSUInteger *)index
{
    CGRect miniFrame = CGRectMake(0, 0, 0, 200000);
    for (int i = 0; i < self.columns; i++) {
        NSValue *value = self.frameArray[i];
        if (CGRectGetMaxY([value CGRectValue]) < CGRectGetMaxY(miniFrame)) {
            miniFrame = [value CGRectValue];
            *index = i;
        }
    }
    return miniFrame;
}

- (CGRect)maximumHeight
{
    CGRect maxFrame = CGRectMake(0, 0, 0, 0);
    for (NSValue *value in self.frameArray) {
        if (CGRectGetMaxY([value CGRectValue]) > CGRectGetMaxY(maxFrame)) {
            maxFrame = [value CGRectValue];
        }
    }
    return maxFrame;
}

// 准备好每一个item存放的位置
- (void)prepareLayout
{
    [super prepareLayout];
    [self.attributesArray removeAllObjects];
    CGFloat margin = 20;
    CGFloat padding = 10;
    CGFloat itemWidth = (self.collectionView.bounds.size.width - 2 * margin - ((self.columns - 1) * padding)) / self.columns;
    
    for (NSUInteger i = 0; i < self.columns; i++) {
        CGRect frame = CGRectMake((itemWidth + padding) * i + self.sectionInset.left, 0, itemWidth, self.sectionInset.top + self.headerReferenceSize.height);
        [self.frameArray addObject:[NSValue valueWithCGRect:frame]];
    }
    
    NSInteger itemSection = 1;
    if ([self.collectionView.dataSource respondsToSelector:@selector(numberOfSectionsInCollectionView:)]) {
        itemSection = [self.collectionView.dataSource numberOfSectionsInCollectionView:self.collectionView];
    }
    
    // 确定每一个item存放的位置
    for (NSInteger i = 0; i < itemSection; i++) {
        NSInteger itemNumber = [self.collectionView.dataSource collectionView:self.collectionView numberOfItemsInSection:i];
        for (NSInteger j = 0; j < itemNumber; j++) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:j inSection:i];
            CGSize itemSize = [self.delegate collectionView:self.collectionView layout:self sizeForItemAtIndexPath:indexPath];
            CGFloat itemHeight = itemSize.height * itemWidth / itemSize.width;
            
            // 获取所有列中高度最小的列的frame
            NSUInteger index = 0;
            CGRect miniFrame = [self minimumHeightOfIndex:&index];
            CGFloat X = miniFrame.origin.x;
            CGFloat Y = miniFrame.size.height;
            
            // 确定item的frame
            CGRect itemFrame = CGRectMake(X, Y, itemWidth, itemHeight);
            
            // 将frame封装成UICollectionViewLayoutAttributes对象
            UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
            layoutAttributes.frame = itemFrame;
            [self.attributesArray addObject:layoutAttributes];
            
            // 更新数组
            CGRect newFrame = CGRectMake(miniFrame.origin.x, 0, itemWidth, Y + itemHeight + self.minimumLineSpacing);
            [self.frameArray replaceObjectAtIndex:index withObject:[NSValue valueWithCGRect:newFrame]];
        }
    }
}

// 返回collectionView可滑动的区域
- (CGSize)collectionViewContentSize
{
    CGRect maxFrame = [self maximumHeight];
    CGSize contentSize = CGSizeMake(0, maxFrame.size.height + self.sectionInset.bottom);
    return contentSize;
}

// 返回所有item的layoutAttributes
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    return self.attributesArray;
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath
{
    if ([elementKind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
        layoutAttributes.frame = CGRectMake(0, 0, self.headerReferenceSize.width, self.headerReferenceSize.height);
        return layoutAttributes;
    }
    return nil;
}

@end
